package com.bs.sc.recommend.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Optional;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.bs.sc.base.utils.AjaxJson;
import com.bs.sc.base.utils.BasedUserRecommendation;
import com.bs.sc.base.utils.FileUtil;
import com.bs.sc.front.goodsCar.dao.GoodsCarDao;
import com.bs.sc.front.goodsCar.dao.GoodsCarItemDao;
import com.bs.sc.front.goodsCar.entity.GoodsCar;
import com.bs.sc.front.user.entity.User;
import com.bs.sc.mgr.goods.dao.GoodsDao;
import com.bs.sc.mgr.goods.entity.Goods;
import com.bs.sc.mgr.order.dao.OrderDao;
import com.bs.sc.mgr.order.dao.OrderItemDao;
import com.bs.sc.mgr.order.entity.Order;
import com.bs.sc.recommend.service.RecommendService;

@Service("recommendService")
@Transactional
public class RecommendServiceImpl implements RecommendService {

	@Autowired
	private OrderDao orderDao;
	@Autowired
	private GoodsCarDao goodsCarDao;
	@Autowired
	private GoodsCarItemDao goodsCarItemDao;
	@Autowired
	private GoodsDao goodsDao;
	@Autowired
	private OrderItemDao orderItemDao;

	/**
	 * 获取所有购物车列表
	 */
	public List<GoodsCar> findAllGoodsCar() {
		List<GoodsCar> cars = goodsCarDao.findAll();
		return cars;
	}

	/**
	 * 获取所有订单列表
	 * 
	 * @return
	 */
	public List<Order> findAllOrder() {
		List<Order> orders = orderDao.findAll();
		return orders;
	}

	/**
	 * 分析数据并生成TXT文件
	 */
	public boolean analyseData() {
		Set<String> set = new HashSet<>();
		List<Map<String, Object>> list = goodsCarItemDao.findAllBuyGoods();
		for (Map<String, Object> reslut : list) {
			String setData = reslut.get("userId") + "," + reslut.get("goodsId") + "," + reslut.get("grade");
			set.add(setData);
		}
		return FileUtil.createTxt(set, 0);
	}

	@Override
	public AjaxJson findRecommendGoodsList(HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		j.setCode("failure");
		j.setMsg("获取推荐商品列表失败");
		try {
			User user = (User) request.getSession().getAttribute("user");
			System.out.println("获取推荐商品，登陆用户为：" + user);
			if (user == null) {
				j.setMsg("请登录");
				return j;
			}
			List<Map<String, String>> result = BasedUserRecommendation.userRecommendation(user.getId(), goodsDao);
			System.out.println("推荐商品：" + result.toString());
			if (result == null || result.size() <= 0) {
				System.out.println("为用户【" + user.getUserName() + "】推荐的商品数量为：" + 0);
				j.setMsg("没有推荐商品");
				return j;
			}
			System.out.println("为用户【" + user.getUserName() + "】推荐的商品数量为：" + result.size());
			List<Map<String, Object>> goodsList = new ArrayList<>();
			// 遍历推荐的商品id
			for (Map<String, String> m : result) {
				Optional<Goods> optional = goodsDao.findById(m.get("goodsId"));
				Map<String, Object> retMap = new HashMap<>();
				retMap.put("goods", optional.get());
				retMap.put("grade", m.get("grade"));
				goodsList.add(retMap);
			}
			j.setCode("success");
			j.setMsg("获取推荐商品列表成功");
			j.setData(goodsList);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return j;
	}

	@Override
	public boolean analyseClassfiyData() {
		Set<String> set = new HashSet<>();
		List<Map<String, Object>> list = orderItemDao.findUserClassifyGrade();
		Map<String, String> temp = new HashMap<String, String>();
		for (Map<String, Object> map : list) {
			String userId = (String) map.get("user_id");
			String classify = String.valueOf(map.get("classify"));
			String grade = String.valueOf(map.get("grade"));
			String tempKey = userId + "," + classify;
			if (temp.containsKey(tempKey)) {
				int va = Integer.valueOf(temp.get(tempKey));
				int newVa = Integer.valueOf(grade);
				if (newVa > va) {
					temp.put(tempKey, String.valueOf(newVa));
				}
			} else {
				temp.put(tempKey, grade);
			}
		}
		for (Entry<String, String> en : temp.entrySet()) {
			String key = en.getKey();
			String value = en.getValue();
			String s = key + "," + value;
			set.add(s);
		}
		return FileUtil.createTxt(set, 1);
	}
}
